<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - svm_ex.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
</font><font color='#009900'>/*

    This is an example illustrating the use of the support vector machine
    utilities from the dlib C++ Library.  

    This example creates a simple set of data to train on and then shows
    you how to use the cross validation and svm training functions
    to find a good decision function that can classify examples in our
    data set.


    The data used in this example will be 2 dimensional data and will
    come from a distribution where points with a distance less than 10
    from the origin are labeled +1 and all other points are labeled
    as -1.
        
*/</font>


<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>svm.h<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;


<font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
    <font color='#009900'>// The svm functions use column vectors to contain a lot of the data on which they
</font>    <font color='#009900'>// operate. So the first thing we do here is declare a convenient typedef.  
</font>
    <font color='#009900'>// This typedef declares a matrix with 2 rows and 1 column.  It will be the object that
</font>    <font color='#009900'>// contains each of our 2 dimensional samples.   (Note that if you wanted more than 2
</font>    <font color='#009900'>// features in this vector you can simply change the 2 to something else.  Or if you
</font>    <font color='#009900'>// don't know how many features you want until runtime then you can put a 0 here and
</font>    <font color='#009900'>// use the matrix.set_size() member function)
</font>    <font color='#0000FF'>typedef</font> matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>, <font color='#979000'>2</font>, <font color='#979000'>1</font><font color='#5555FF'>&gt;</font> sample_type;

    <font color='#009900'>// This is a typedef for the type of kernel we are going to use in this example.  In
</font>    <font color='#009900'>// this case I have selected the radial basis kernel that can operate on our 2D
</font>    <font color='#009900'>// sample_type objects
</font>    <font color='#0000FF'>typedef</font> radial_basis_kernel<font color='#5555FF'>&lt;</font>sample_type<font color='#5555FF'>&gt;</font> kernel_type;


    <font color='#009900'>// Now we make objects to contain our samples and their respective labels.
</font>    std::vector<font color='#5555FF'>&lt;</font>sample_type<font color='#5555FF'>&gt;</font> samples;
    std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font> labels;

    <font color='#009900'>// Now let's put some data into our samples and labels objects.  We do this by looping
</font>    <font color='#009900'>// over a bunch of points and labeling them according to their distance from the
</font>    <font color='#009900'>// origin.
</font>    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> r <font color='#5555FF'>=</font> <font color='#5555FF'>-</font><font color='#979000'>20</font>; r <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>20</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>r<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> c <font color='#5555FF'>=</font> <font color='#5555FF'>-</font><font color='#979000'>20</font>; c <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>20</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>c<font face='Lucida Console'>)</font>
        <b>{</b>
            sample_type samp;
            <font color='#BB00BB'>samp</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> r;
            <font color='#BB00BB'>samp</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> c;
            samples.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font>samp<font face='Lucida Console'>)</font>;

            <font color='#009900'>// if this point is less than 10 from the origin
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>sqrt</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font><font face='Lucida Console'>)</font>r<font color='#5555FF'>*</font>r <font color='#5555FF'>+</font> c<font color='#5555FF'>*</font>c<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>10</font><font face='Lucida Console'>)</font>
                labels.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font><font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>else</font>
                labels.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font><font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;

        <b>}</b>
    <b>}</b>


    <font color='#009900'>// Here we normalize all the samples by subtracting their mean and dividing by their
</font>    <font color='#009900'>// standard deviation.  This is generally a good idea since it often heads off
</font>    <font color='#009900'>// numerical stability problems and also prevents one large feature from smothering
</font>    <font color='#009900'>// others.  Doing this doesn't matter much in this example so I'm just doing this here
</font>    <font color='#009900'>// so you can see an easy way to accomplish this with the library.  
</font>    vector_normalizer<font color='#5555FF'>&lt;</font>sample_type<font color='#5555FF'>&gt;</font> normalizer;
    <font color='#009900'>// let the normalizer learn the mean and standard deviation of the samples
</font>    normalizer.<font color='#BB00BB'>train</font><font face='Lucida Console'>(</font>samples<font face='Lucida Console'>)</font>;
    <font color='#009900'>// now normalize each sample
</font>    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        samples[i] <font color='#5555FF'>=</font> <font color='#BB00BB'>normalizer</font><font face='Lucida Console'>(</font>samples[i]<font face='Lucida Console'>)</font>; 


    <font color='#009900'>// Now that we have some data we want to train on it.  However, there are two
</font>    <font color='#009900'>// parameters to the training.  These are the nu and gamma parameters.  Our choice for
</font>    <font color='#009900'>// these parameters will influence how good the resulting decision function is.  To
</font>    <font color='#009900'>// test how good a particular choice of these parameters is we can use the
</font>    <font color='#009900'>// cross_validate_trainer() function to perform n-fold cross validation on our training
</font>    <font color='#009900'>// data.  However, there is a problem with the way we have sampled our distribution
</font>    <font color='#009900'>// above.  The problem is that there is a definite ordering to the samples.  That is,
</font>    <font color='#009900'>// the first half of the samples look like they are from a different distribution than
</font>    <font color='#009900'>// the second half.  This would screw up the cross validation process but we can fix it
</font>    <font color='#009900'>// by randomizing the order of the samples with the following function call.
</font>    <font color='#BB00BB'>randomize_samples</font><font face='Lucida Console'>(</font>samples, labels<font face='Lucida Console'>)</font>;


    <font color='#009900'>// The nu parameter has a maximum value that is dependent on the ratio of the +1 to -1
</font>    <font color='#009900'>// labels in the training data.  This function finds that value.
</font>    <font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> max_nu <font color='#5555FF'>=</font> <font color='#BB00BB'>maximum_nu</font><font face='Lucida Console'>(</font>labels<font face='Lucida Console'>)</font>;

    <font color='#009900'>// here we make an instance of the svm_nu_trainer object that uses our kernel type.
</font>    svm_nu_trainer<font color='#5555FF'>&lt;</font>kernel_type<font color='#5555FF'>&gt;</font> trainer;

    <font color='#009900'>// Now we loop over some different nu and gamma values to see how good they are.  Note
</font>    <font color='#009900'>// that this is a very simple way to try out a few possible parameter choices.  You
</font>    <font color='#009900'>// should look at the <a href="model_selection_ex.cpp.html">model_selection_ex.cpp</a> program for examples of more sophisticated
</font>    <font color='#009900'>// strategies for determining good parameter choices.
</font>    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>doing cross validation</font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> gamma <font color='#5555FF'>=</font> <font color='#979000'>0.00001</font>; gamma <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>1</font>; gamma <font color='#5555FF'>*</font><font color='#5555FF'>=</font> <font color='#979000'>5</font><font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> nu <font color='#5555FF'>=</font> <font color='#979000'>0.00001</font>; nu <font color='#5555FF'>&lt;</font> max_nu; nu <font color='#5555FF'>*</font><font color='#5555FF'>=</font> <font color='#979000'>5</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#009900'>// tell the trainer the parameters we want to use
</font>            trainer.<font color='#BB00BB'>set_kernel</font><font face='Lucida Console'>(</font><font color='#BB00BB'>kernel_type</font><font face='Lucida Console'>(</font>gamma<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            trainer.<font color='#BB00BB'>set_nu</font><font face='Lucida Console'>(</font>nu<font face='Lucida Console'>)</font>;

            cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>gamma: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> gamma <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>    nu: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> nu;
            <font color='#009900'>// Print out the cross validation accuracy for 3-fold cross validation using
</font>            <font color='#009900'>// the current gamma and nu.  cross_validate_trainer() returns a row vector.
</font>            <font color='#009900'>// The first element of the vector is the fraction of +1 training examples
</font>            <font color='#009900'>// correctly classified and the second number is the fraction of -1 training
</font>            <font color='#009900'>// examples correctly classified.
</font>            cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>     cross validation accuracy: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>cross_validate_trainer</font><font face='Lucida Console'>(</font>trainer, samples, labels, <font color='#979000'>3</font><font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>


    <font color='#009900'>// From looking at the output of the above loop it turns out that a good value for nu
</font>    <font color='#009900'>// and gamma for this problem is 0.15625 for both.  So that is what we will use.
</font>
    <font color='#009900'>// Now we train on the full set of data and obtain the resulting decision function.  We
</font>    <font color='#009900'>// use the value of 0.15625 for nu and gamma.  The decision function will return values
</font>    <font color='#009900'>// &gt;= 0 for samples it predicts are in the +1 class and numbers &lt; 0 for samples it
</font>    <font color='#009900'>// predicts to be in the -1 class.
</font>    trainer.<font color='#BB00BB'>set_kernel</font><font face='Lucida Console'>(</font><font color='#BB00BB'>kernel_type</font><font face='Lucida Console'>(</font><font color='#979000'>0.15625</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    trainer.<font color='#BB00BB'>set_nu</font><font face='Lucida Console'>(</font><font color='#979000'>0.15625</font><font face='Lucida Console'>)</font>;
    <font color='#0000FF'>typedef</font> decision_function<font color='#5555FF'>&lt;</font>kernel_type<font color='#5555FF'>&gt;</font> dec_funct_type;
    <font color='#0000FF'>typedef</font> normalized_function<font color='#5555FF'>&lt;</font>dec_funct_type<font color='#5555FF'>&gt;</font> funct_type;

    <font color='#009900'>// Here we are making an instance of the normalized_function object.  This object
</font>    <font color='#009900'>// provides a convenient way to store the vector normalization information along with
</font>    <font color='#009900'>// the decision function we are going to learn.  
</font>    funct_type learned_function;
    learned_function.normalizer <font color='#5555FF'>=</font> normalizer;  <font color='#009900'>// save normalization information
</font>    learned_function.function <font color='#5555FF'>=</font> trainer.<font color='#BB00BB'>train</font><font face='Lucida Console'>(</font>samples, labels<font face='Lucida Console'>)</font>; <font color='#009900'>// perform the actual SVM training and save the results
</font>
    <font color='#009900'>// print out the number of support vectors in the resulting decision function
</font>    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\nnumber of support vectors in our learned_function is </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> learned_function.function.basis_vectors.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#009900'>// Now let's try this decision_function on some samples we haven't seen before.
</font>    sample_type sample;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>3.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>2</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This is a +1 class example, the classifier output is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_function</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>3.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>9.3545</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This is a +1 class example, the classifier output is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_function</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>13.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>9.3545</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This is a -1 class example, the classifier output is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_function</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>13.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This is a -1 class example, the classifier output is </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_function</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;


    <font color='#009900'>// We can also train a decision function that reports a well conditioned probability
</font>    <font color='#009900'>// instead of just a number &gt; 0 for the +1 class and &lt; 0 for the -1 class.  An example
</font>    <font color='#009900'>// of doing that follows:
</font>    <font color='#0000FF'>typedef</font> probabilistic_decision_function<font color='#5555FF'>&lt;</font>kernel_type<font color='#5555FF'>&gt;</font> probabilistic_funct_type;  
    <font color='#0000FF'>typedef</font> normalized_function<font color='#5555FF'>&lt;</font>probabilistic_funct_type<font color='#5555FF'>&gt;</font> pfunct_type;

    pfunct_type learned_pfunct; 
    learned_pfunct.normalizer <font color='#5555FF'>=</font> normalizer;
    learned_pfunct.function <font color='#5555FF'>=</font> <font color='#BB00BB'>train_probabilistic_decision_function</font><font face='Lucida Console'>(</font>trainer, samples, labels, <font color='#979000'>3</font><font face='Lucida Console'>)</font>;
    <font color='#009900'>// Now we have a function that returns the probability that a given sample is of the +1 class.  
</font>
    <font color='#009900'>// print out the number of support vectors in the resulting decision function.  
</font>    <font color='#009900'>// (it should be the same as in the one above)
</font>    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\nnumber of support vectors in our learned_pfunct is </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> learned_pfunct.function.decision_funct.basis_vectors.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>3.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>2</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This +1 class example should have high probability.  Its probability is: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_pfunct</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>3.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>9.3545</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This +1 class example should have high probability.  Its probability is: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_pfunct</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>13.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>9.3545</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This -1 class example should have low probability.  Its probability is: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_pfunct</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>13.123</font>;
    <font color='#BB00BB'>sample</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>This -1 class example should have low probability.  Its probability is: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>learned_pfunct</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;



    <font color='#009900'>// Another thing that is worth knowing is that just about everything in dlib is
</font>    <font color='#009900'>// serializable.  So for example, you can save the learned_pfunct object to disk and
</font>    <font color='#009900'>// recall it later like so:
</font>    <font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>saved_function.dat</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> learned_pfunct;

    <font color='#009900'>// Now let's open that file back up and load the function object it contains.
</font>    <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>saved_function.dat</font>"<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font> learned_pfunct;

    <font color='#009900'>// Note that there is also an example program that comes with dlib called the
</font>    <font color='#009900'>// <a href="file_to_code_ex.cpp.html">file_to_code_ex.cpp</a> example.  It is a simple program that takes a file and outputs a
</font>    <font color='#009900'>// piece of C++ code that is able to fully reproduce the file's contents in the form of
</font>    <font color='#009900'>// a std::string object.  So you can use that along with the std::istringstream to save
</font>    <font color='#009900'>// learned decision functions inside your actual C++ code files if you want.  
</font>



    <font color='#009900'>// Lastly, note that the decision functions we trained above involved well over 200
</font>    <font color='#009900'>// basis vectors.  Support vector machines in general tend to find decision functions
</font>    <font color='#009900'>// that involve a lot of basis vectors.  This is significant because the more basis
</font>    <font color='#009900'>// vectors in a decision function, the longer it takes to classify new examples.  So
</font>    <font color='#009900'>// dlib provides the ability to find an approximation to the normal output of a trainer
</font>    <font color='#009900'>// using fewer basis vectors.  
</font>
    <font color='#009900'>// Here we determine the cross validation accuracy when we approximate the output using
</font>    <font color='#009900'>// only 10 basis vectors.  To do this we use the reduced2() function.  It takes a
</font>    <font color='#009900'>// trainer object and the number of basis vectors to use and returns a new trainer
</font>    <font color='#009900'>// object that applies the necessary post processing during the creation of decision
</font>    <font color='#009900'>// function objects.
</font>    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\ncross validation accuracy with only 10 support vectors: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>cross_validate_trainer</font><font face='Lucida Console'>(</font><font color='#BB00BB'>reduced2</font><font face='Lucida Console'>(</font>trainer,<font color='#979000'>10</font><font face='Lucida Console'>)</font>, samples, labels, <font color='#979000'>3</font><font face='Lucida Console'>)</font>;

    <font color='#009900'>// Let's print out the original cross validation score too for comparison.
</font>    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>cross validation accuracy with all the original support vectors: </font>" 
         <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>cross_validate_trainer</font><font face='Lucida Console'>(</font>trainer, samples, labels, <font color='#979000'>3</font><font face='Lucida Console'>)</font>;

    <font color='#009900'>// When you run this program you should see that, for this problem, you can reduce the
</font>    <font color='#009900'>// number of basis vectors down to 10 without hurting the cross validation accuracy. 
</font>

    <font color='#009900'>// To get the reduced decision function out we would just do this:
</font>    learned_function.function <font color='#5555FF'>=</font> <font color='#BB00BB'>reduced2</font><font face='Lucida Console'>(</font>trainer,<font color='#979000'>10</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>train</font><font face='Lucida Console'>(</font>samples, labels<font face='Lucida Console'>)</font>;
    <font color='#009900'>// And similarly for the probabilistic_decision_function: 
</font>    learned_pfunct.function <font color='#5555FF'>=</font> <font color='#BB00BB'>train_probabilistic_decision_function</font><font face='Lucida Console'>(</font><font color='#BB00BB'>reduced2</font><font face='Lucida Console'>(</font>trainer,<font color='#979000'>10</font><font face='Lucida Console'>)</font>, samples, labels, <font color='#979000'>3</font><font face='Lucida Console'>)</font>;
<b>}</b>


</pre></body></html>